package lolxxs.chenyanting.controller;


import lolxxs.chenyanting.entity.Result;
import lolxxs.chenyanting.entity.StatusCode;
import lolxxs.chenyanting.model.ResumeInformation;
import lolxxs.chenyanting.service.IResumeInformationService;
import lolxxs.chenyanting.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-12-30
 */
@RestController
@RequestMapping("/resume-information")
public class ResumeInformationController {
    @Autowired
    @Qualifier(value = "resumeInformationServiceImpl")
    private IResumeInformationService resumeInformationService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/add")
    public Result add(HttpServletRequest request, @RequestBody ResumeInformation resumeInformation) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
//             && (Integer)claims.get("identity") >=  1 添加简历无须HR权限
            if (claims.getId() != null && claims.getSubject() != null) {
                resumeInformation.setUserId(Long.valueOf(claims.getId()));
                resumeInformation.setUserName(claims.getSubject());// 用户名
                int row = resumeInformationService.add(resumeInformation);
                if (row > 0) {
                    return new Result(true, StatusCode.OK, "添加成功", row);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "添加失败");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }


    @PostMapping("/update")
    public Result update(HttpServletRequest request, @RequestBody ResumeInformation resumeInformation) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                int row = resumeInformationService.update(resumeInformation);
                if (row > 0) {
                    return new Result(false, StatusCode.OK, "修改成功");
                }
            }
        }
        return new Result(true, StatusCode.UPDATEERROR, "修改失败");
    }

    @PostMapping("/query")
    public Result query(HttpServletRequest request, @RequestBody ResumeInformation resumeInformation) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null) {
                if ((Integer)claims.get("identity") <  1) {
                    resumeInformation.setUserId(Long.valueOf(claims.getId()));
                }
                List<ResumeInformation> query = resumeInformationService.query(resumeInformation);
                if (query != null && query.size() > 0) {
                    return new Result(true, StatusCode.OK, "查询成功",query);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }

    @PostMapping("/delete")
    public Result delete(HttpServletRequest request, @RequestBody ResumeInformation resumeInformation) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                int row = resumeInformationService.delete(resumeInformation);
                if (row > 0) {
                    return new Result(true, StatusCode.OK, "删除成功", row);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }
}
